package com.zingrow.web.report.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.zingrow.common.exception.NumberErrorException;
import com.zingrow.web.jms.JMSProducer;
import com.zingrow.web.report.base.Cycle;
import com.zingrow.web.report.model.RptInfo;
import com.zingrow.web.report.service.IRptInfoService;
import com.zingrow.web.report.service.IRptScheduleTask;
import com.zingrow.web.report.service.IRptStatisticsService;
import com.zingrow.web.report.utils.DateUtils;
import com.zzyq.dataManager.bean.MessageText;

@Service
public class RptScheduleTaskImpl implements IRptScheduleTask {
	@Autowired
	private IRptInfoService RptInfoService;

	@Autowired
	private IRptStatisticsService rptStatisticsService;
	

	// 每日凌晨8点触发
	@Scheduled(cron = "0 0 8  * * ? ")
	@Override
	public void scheduleDayRpt() {
		// TODO Auto-generated method stub
		Date createTime = new Date();
		Calendar calendar = DateUtils.getZero(createTime);
		calendar.add(Calendar.DATE, -1);
		Date startTime = calendar.getTime();
		List<RptInfo> RptInfoList = RptInfoService.createDayRpt(createTime, startTime);
		for (RptInfo rptInfo : RptInfoList) {
			//推送
			MessageText message = new MessageText();
			message.setIfmId("0");
			message.setArticleId("0");
				message.setTitle(rptInfo.getName());
				message.setType(1);
				message.setRepId(rptInfo.getId());
				message.setModuleSort(rptInfo.getModuleSort());
				try {
					JMSProducer.sendMessage(message);
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
	}

	// 每周一凌晨8点2分触发
	@Scheduled(cron = "0 2 8 ? * MON")
	@Override
	public void scheduleWeekRpt() {
		// TODO Auto-generated method stub
		Date createTime = new Date();
		Calendar calendar = DateUtils.getZero(createTime);
		calendar.add(Calendar.DATE, -7);
		DateUtils.setToFirstDay(calendar);
		Date startTime = calendar.getTime();
		List<RptInfo> RptInfoList = RptInfoService.createWeekRpt(createTime, startTime);
		for (RptInfo rptInfo : RptInfoList) {
			//推送
			MessageText message = new MessageText();
			message.setIfmId("0");
			message.setArticleId("0");
				message.setTitle(rptInfo.getName());
				message.setType(1);
				message.setRepId(rptInfo.getId());
				message.setModuleSort(rptInfo.getModuleSort());
				try {
					JMSProducer.sendMessage(message);
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
	}

	// 每月第一天凌晨8点4分触发
	@Scheduled(cron = "0 4 8 1 * ? ")
	@Override
	public void scheduleMonthRpt() {
		// TODO Auto-generated method stub
		Date createTime = new Date();
		Calendar calendar = DateUtils.getZero(createTime);
		calendar.add(Calendar.MONTH, -1);
		Date startTime = calendar.getTime();
		List<RptInfo> RptInfoList = RptInfoService.createMonthRpt(createTime, startTime);
		for (RptInfo rptInfo : RptInfoList) {
			//推送
			MessageText message = new MessageText();
			message.setIfmId("0");
			message.setArticleId("0");
				message.setTitle(rptInfo.getName());
				message.setType(1);
				message.setRepId(rptInfo.getId());
				message.setModuleSort(rptInfo.getModuleSort());
				try {
					JMSProducer.sendMessage(message);
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
	}

	// 每天凌晨01:20分触发 站点日报
//	@Scheduled(cron = "0 */3 * * * ?")
	@Scheduled(cron = "0 20 1 * * ? ")
	@Override
	public void statisticsDomainDayRpt() {
		List<Integer> nullifnIds = new ArrayList<>();
		Date createTime = new Date();
		Calendar calendar = DateUtils.getZero(createTime);
		Date endTime = calendar.getTime();
		calendar.add(Calendar.DATE, -1);
		Date startTime = calendar.getTime();
		try {
			rptStatisticsService.insterDimensionDomain(nullifnIds, startTime, endTime, Cycle.DAY.getCode());
		} catch (NumberErrorException e) {
			e.printStackTrace();
		}
	}

	// 每周一凌晨01:30分触发 站点周报
	@Scheduled(cron = "0 30 1 ? * MON")
	@Override
	public void statisticsDomainWeekRpt() {
		List<Integer> nullifnIds = new ArrayList<>();
		Date createTime = new Date();
		Calendar calendar = DateUtils.getZero(createTime);
		Date endTime = calendar.getTime();
		calendar.add(Calendar.DATE, -7);
		Date startTime = calendar.getTime();
		try {
			rptStatisticsService.insterDimensionDomain(nullifnIds, startTime, endTime, Cycle.WEEK.getCode());
		} catch (NumberErrorException e) {
			e.printStackTrace();
		}
	}

	// 每月一号凌晨01:45分触发 站点月报
//	 @Scheduled(cron = "0 */3 * * * ?")
	@Scheduled(cron = "0 45 1 1 * ? ")
	@Override
	public void statisticsDomainMonthRpt() {
		List<Integer> nullifnIds = new ArrayList<>();
		Date createTime = new Date();
		Calendar calendar = DateUtils.getZero(createTime);
		Date endTime = calendar.getTime();
		calendar.add(Calendar.MONTH, -1);
		Date startTime = calendar.getTime();
		try {
			rptStatisticsService.insterDimensionDomain(nullifnIds, startTime, endTime, Cycle.MONTH.getCode());
		} catch (NumberErrorException e) {
			e.printStackTrace();
		}

	}

	// 每天凌晨01:12分触发 渠道
//	 @Scheduled(cron = "0 */3 * * * ?")
	@Scheduled(cron = "0 12 1 * * ? ")
	@Override
	public void statisticsChannelDayRpt() {
		List<Integer> nullifnIds = new ArrayList<>();
		try {
			rptStatisticsService.insterChannel(nullifnIds, null);
		} catch (NumberErrorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 每天凌晨01:00分触发 方案
	@Scheduled(cron = "0 0 1 * * ? ")
	@Override
	public void statisticsPlan() {
		List<Integer> nullifnIds = new ArrayList<>();
		try {
			rptStatisticsService.insterPlan(nullifnIds, null);
		} catch (NumberErrorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	// 每天凌晨01:02分触发 方案排行
	@Scheduled(cron = "0 2 1 * * ? ")
	@Override
	public void statisticsPlanRanking() {
		List<Integer> nullifnIds = new ArrayList<>();
		try {
			rptStatisticsService.insterPlanRanking(nullifnIds, null);
		} catch (NumberErrorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 每天凌晨01:04分触发 新闻排行
	@Scheduled(cron = "0 4 1 * * ? ")
	@Override
	public void statisticsNewsRanking() {
		List<Integer> nullifnIds = new ArrayList<>();
		try {
			rptStatisticsService.insterNewsRanking(nullifnIds, null);
		} catch (NumberErrorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 每天凌晨01:06分触发 微信排行
	@Scheduled(cron = "0 6 1 * * ? ")
	@Override
	public void statisticsWeixinRanking() {
		List<Integer> nullifnIds = new ArrayList<>();
		try {
			rptStatisticsService.insterWeixinRanking(nullifnIds, null);
		} catch (NumberErrorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 每天凌晨01:08分触发 微博排行
	@Scheduled(cron = "0 8 1 * * ? ")
	@Override
	public void statisticsWeiboRanking() {
		List<Integer> nullifnIds = new ArrayList<>();
		try {
			rptStatisticsService.insterWeiboRanking(nullifnIds, null);
		} catch (NumberErrorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 每天凌晨01:10分触发 论坛排行
	@Scheduled(cron = "0 10 1 * * ? ")
	@Override
	public void statisticsBbsRanking() {
		List<Integer> nullifnIds = new ArrayList<>();
		try {
			rptStatisticsService.insterBbsRanking(nullifnIds, null);
		} catch (NumberErrorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Date createTime = new Date();
		Calendar calendar = DateUtils.getZero(createTime);
		Date endTime = calendar.getTime();
		calendar.add(Calendar.MONTH, -1);
		Date startTime = calendar.getTime();

		System.out.println(startTime);
		System.out.println(endTime);
	}
}
